package com.dygg.bigfish.dao;

import com.dygg.bigfish.entity.User;
import com.dygg.bigfish.util.JDBCUtils;
//import com.sun.org.apache.bcel.internal.generic.LUSHR;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * 注册功能
     *
     * @param username
     * @param password
     * @return
     */
    public boolean addUser(String username, String password) {
        String sql = "insert into bigfish_user(user_name,user_password,user_authority) values(?,?,0)";
        int count = JDBCUtils.doUpdate(sql, username, password);
        System.out.println(count);
        return count > 0;
    }

    public User login(String username, String userpassword) {
        String sql = "select * from bigfish_user where user_name=? and user_password=?";
        ResultSet rs = JDBCUtils.doQuery(sql, username, userpassword);
        User user = null;
        try {
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("user_id"));
                user.setUsername(rs.getString("user_name"));
                user.setPassword(rs.getString("user_password"));
                user.setAuthority(rs.getInt("user_authority"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }


    public boolean modify(String id,String newPassword) {
        String sql = "update bigfish_user set user_password = ?  where user_id = ? ";
        int t = JDBCUtils.doUpdate(sql,newPassword,id);
        return t > 0;
    }
    public String queryUserID(String id) {
        String sql = "select user_password from bigfish_user where user_id=? ";
        ResultSet resultSet = JDBCUtils.doQuery(sql, id);
        String msg = "";
        try {
            while (resultSet.next()){
               msg =  resultSet.getString("user_password");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return msg;
    }

    public boolean queryAll(String username) {
        List<User> users = new ArrayList<>();
        boolean flag = true;
        String sql = "select * from bigfish_user where user_name=? ";
        ResultSet rs = JDBCUtils.doQuery(sql,username);
        try {
            if (rs.next()){
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.doClose(rs);
        }
        return flag;
    }
}
